<?php
namespace app\controller\manager;

use app\BaseController;
use app\model\VStudentCourse;
use app\model\StudentCourse;
use app\model\Course;
use app\model\Student;
use app\utils\Excel;
use think\Request;
use \think\facade\Db;
use Exception;

/**
 * 学生-课程管理控制器
 * @todo 完善增删改查
 * @todo 完善excel导入
 */
class StudentCourseManage extends BaseController
{
    
    public function list(Request $request){
        $courseID = $request->param("courseID",'not null');
        $pageSize = $request->param("pageSize",10);
        $studentCourse = VStudentCourse::where('courseID',$courseID)->paginate($pageSize);
        return resultSuccess($studentCourse);
    }

    /**
     * 课程增加学生
     */
    public function add(Request $request){
        if ($request->isPost()) {
            $studentCourse = new StudentCourse;
            if ($studentCourse->save(
                [
                    'SID' => $request->param('SID'),
                    'courseID' => $request->param('courseID'),
                ]
            )) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 课程按班级添加学生
     */
    public function addClase(){
        $claseID = $this->request->param('claseID');
        $courseID = $this->request->param("courseID");
        if($courseID == null || $claseID==null){
            return resultError('参数不足');
        }
        $course = Course::find($courseID);
        if($course == null){
            return resultError('无此班级');
        }
        $studentClase = Student::where('claseID',$claseID)->select()->toArray();
        $SIDList = array_column($studentClase,'SID');
        $list = [];
        foreach($SIDList as $SID){
            $list[]=[
                'SID' => $SID,
                'courseID'=>$courseID
            ];
        }
        if(Db::name('studentcourse')->replace()->insertAll($list) == sizeof($SIDList) ){
            return resultSuccess();
        }
        return resultError('未能插入全部数据');
        
    }

    /**
     * 使用excel添加
     */
    /**
     * Excel插入学生(覆盖插入)
     */
    public function excelInsertStudent(Request $request){
        $courseID = $request->param('courseID');
        if(!$courseID) return resultError('需要课程ID');
        $files = $request->file("file");
        $data = Excel::importExcel($files);
        $insertList = [];
        foreach($data as $dt){
            $insertList[]=[
                'SID' => $dt[0],
                'courseID' => $courseID
            ];
        }
        try{
        if(Db::name('studentcourse')->replace()->insertAll($insertList) != sizeof($insertList)){
            return resultError('未能全部插入');
        }
        }
        catch( Exception $e){
            return resultError('格式错误');
        }
        return resultSuccess();
    }

    /**
     * 课程删除该学生
     */
    public function delete(Request $request){
        $courseID = $request->param("courseID");
        $SID = $request->param("SID");
        if (Db::name('studentcourse')->where(['SID'=>$SID,'courseID'=>$courseID])->delete()) {
            return resultSuccess();
        } else {
            return resultError();
        }
    }
    
}
